﻿#region COPYRIGHT© 2009-2013 Phillip Clark. All rights reserved.

// For licensing information see License.txt (MIT style licensing).

#endregion

using System;

namespace FlitBit.Json
{
	public sealed class JsonBoolean : JsonValue
	{
		public static readonly JsonBoolean False = new JsonBoolean(false);
		public static readonly JsonBoolean True = new JsonBoolean(true);

		bool _value;

		public JsonBoolean(bool value)
			: base(JsonNodeKind.BooleanLiteral)
		{
			_value = value;
		}

		public override TypeCode GetTypeCode()
		{
			return TypeCode.Boolean;
		}

		public override bool ToBoolean(IFormatProvider provider)
		{
			return _value;
		}

		public override char ToChar(IFormatProvider provider)
		{
			return Convert.ToChar(_value);
		}

		public override decimal ToDecimal(IFormatProvider provider)
		{
			return Convert.ToDecimal(_value);
		}

		public override double ToDouble(IFormatProvider provider)
		{
			return Convert.ToDouble(_value);
		}

		public override short ToInt16(IFormatProvider provider)
		{
			return Convert.ToInt16(_value);
		}

		public override int ToInt32(IFormatProvider provider)
		{
			return Convert.ToInt32(_value);
		}

		public override long ToInt64(IFormatProvider provider)
		{
			return Convert.ToInt64(_value);
		}

		public override sbyte ToSByte(IFormatProvider provider)
		{
			return Convert.ToSByte(_value);
		}

		public override float ToSingle(IFormatProvider provider)
		{
			return Convert.ToSingle(_value);
		}

		public override string ToString()
		{
			return (_value) ? "true" : "false";
		}

		public override string ToString(IFormatProvider provider)
		{
			return _value.ToString(provider);
		}

		public override object ToType(Type conversionType, IFormatProvider provider)
		{
			switch (Type.GetTypeCode(conversionType))
			{
				case TypeCode.Boolean:
					return ToBoolean(provider);
				case TypeCode.Byte:
					return ToByte(provider);
				case TypeCode.Char:
					return ToChar(provider);
				case TypeCode.Decimal:
					return ToDecimal(provider);
				case TypeCode.Double:
					return ToDouble(provider);
				case TypeCode.Int16:
					return ToInt16(provider);
				case TypeCode.Int32:
					return ToInt32(provider);
				case TypeCode.Int64:
					return ToInt64(provider);
				case TypeCode.Object:
					return _value;
				case TypeCode.SByte:
					return ToSByte(provider);
				case TypeCode.Single:
					return ToSingle(provider);
				case TypeCode.String:
					return ToString(provider);
				case TypeCode.UInt16:
					return ToUInt16(provider);
				case TypeCode.UInt32:
					return ToUInt32(provider);
				case TypeCode.UInt64:
					return ToUInt64(provider);
			}
			return base.ToType(conversionType, provider);
		}

		public override ushort ToUInt16(IFormatProvider provider)
		{
			return Convert.ToUInt16(_value);
		}

		public override uint ToUInt32(IFormatProvider provider)
		{
			return Convert.ToUInt32(_value);
		}

		public override ulong ToUInt64(IFormatProvider provider)
		{
			return Convert.ToUInt64(_value);
		}
	}
}